<template>
	<el-container v-loading="loading">
		<el-container>
			<el-header>
				<div class="left-panel">
					<div class="left-panel">
						<el-button type="primary" v-auth="'company:order:edit'" icon="el-icon-plus" @click="handleData('add')">新建</el-button>
					</div>
				</div>
			</el-header>
			<el-header>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-select v-model="search.company_id" multiple placeholder="企业客户" style="min-width:14%;" clearable>
							<el-option v-for="item in companyList" :key="item.company_id" :label="item.company_name" :value="item.company_id">
							</el-option>
						</el-select>
						<el-input v-model="search.card_name" placeholder="服务卡名称" style="min-width:13%;" clearable></el-input>
						<el-input v-model="search.product_name" placeholder="商品名称" style="min-width:13%;" clearable></el-input>
						<el-select v-model="search.order_status" multiple placeholder="订单状态" style="min-width:10%;" clearable>
							<el-option v-for="item in orderStatusList" :key="item.order_status" :label="item.order_status_name" :value="item.order_status">
							</el-option>
						</el-select>
						<el-select v-model="search.pay_status" multiple placeholder="支付状态" style="min-width:15%;" clearable>
							<el-option v-for="item in payStatusList" :key="item.pay_status" :label="item.pay_status_name" :value="item.pay_status">
							</el-option>
						</el-select>
						<el-date-picker
							style="min-width:25%;"
							v-model="dateTo"
							type="datetimerange"
							unlink-panels
							range-separator="至"
							start-placeholder="创建时间"
							end-placeholder="结束时间"
							format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss"
							clearable="true"
						/>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
						<el-button type="info" @click="resetsearch">重置</el-button>
					</div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort
						 remoteFilter>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="序号" type="index" width="100"></el-table-column>
					<el-table-column label="订单编号" prop="company_order_sn" width="150"></el-table-column>
					<el-table-column label="企业客户名称" prop="company_name" width="150"></el-table-column>
					<el-table-column label="服务卡名称" prop="card_name" width="150"></el-table-column>
					<el-table-column label="商品名称" prop="card_product_name" width="150"></el-table-column>
					<el-table-column label="订单状态" prop="order_status_name" width="150"></el-table-column>
					<el-table-column label="采购数量" prop="purchase_qty" width="150"></el-table-column>
					<el-table-column label="使用数量" prop="used_qty" width="150"></el-table-column>
					<el-table-column label="采购金额" prop="show_order_amount" width="150"></el-table-column>
					<el-table-column label="支付状态" prop="pay_status_name" width="150"></el-table-column>
					<el-table-column label="支付方式" prop="pay_type" width="150"></el-table-column>
					<el-table-column label="支付金额" prop="show_pay_amount" width="150"></el-table-column>
					<el-table-column label="有效期" prop="valid_time" width="140"></el-table-column>
					<el-table-column label="创建者" prop="created_by" width="150"></el-table-column>
					<el-table-column label="下单时间" prop="created_at" width="150"></el-table-column>
					<el-table-column label="支付时间" prop="pay_at" width="150"></el-table-column>
					<el-table-column label="备注" prop="remark" width="180"></el-table-column>
					<el-table-column label="操作" fixed="right" align="right" width="310">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small"
										   @click="showData('show',scope.row, scope.$index)">查看
								</el-button>
								<el-button v-if="scope.row.allowPay==1" text type="primary" v-auth="''" size="small"
										   @click="payOrder('pay',scope.row, scope.$index)">付款
								</el-button>
								<el-button v-if="scope.row.allowCancel==1" text type="primary" v-auth="''" size="small"
										   @click="cancelOrder('delete',scope.row, scope.$index)">取消订单
								</el-button>
								<el-button v-if="scope.row.allowDownLoadCode==1" text type="primary" v-auth="''" size="small"
										   @click="downLoadCode('download',scope.row, scope.$index)">下载激活码
								</el-button>
								<el-button v-if="scope.row.allowClose==1" text type="primary" v-auth="''" size="small"
										   @click="closeOrder('close',scope.row, scope.$index)">关闭订单
								</el-button>
								<el-button v-if="scope.row.allowImportUser==1" text type="primary" v-auth="''" size="small"
										   @click="importUser('close',scope.row, scope.$index)">导入用户
								</el-button>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
	<paysave-dialog v-if="paydialog.save" ref="paysaveDialog" @success="handleSuccess" @closed="paydialog.save=false"></paysave-dialog>
</template>

<script>
import saveDialog from './save.vue'
import paysaveDialog from './pay_save.vue'

export default {
	name: 'companyOrderIndex',
	components: {
		saveDialog,
		paysaveDialog
	},
	data() {
		return {
			dialog: {
				save: false
			},
			paydialog: {
				save: false
			},
			showGroupLoading: false,
			loading: false,
			apiObj: this.$API['company.company_order'].index,
			selection: [],
			companyList: [],
			orderStatusList: [],
			payStatusList: [],
			//时间搜索
			dateTo: [
				// this.$TOOL.dateFormat(new Date(), 'yyyy-MM-dd') + ' 00:00:00',
				// this.$TOOL.dateFormat(new Date(), 'yyyy-MM-dd') + ' 23:59:59',
			],
			search: {
				company_id: null,
				product_name: null,
				card_name: null,
				order_status: null,
				pay_status: null,
				created_at_begin: null,
				created_at_end: null,
			}
		}
	},
	watch: {
		dateTo(val) {
			if (val) {
				this.search.created_at_begin = val[0];
				this.search.created_at_end = val[1];
			} else {
				this.search.created_at_begin = '';
				this.search.created_at_end = '';
			}
		},
	},
	mounted() {
		//获取单位列表
		this.getCompanyList()
		this.getStatusList()
	},
	methods: {
		getCompanyList() {
			this.$API['company.company'].getCompanyList.get().then((res) => {
				if (res.code == 0) {
					this.companyList = res.data;
				}
			}).catch((msg) => {
				this.$alert(msg, "提示", {type: 'error'})
			})
		},
		getStatusList() {
			this.$API['company.company_order'].getStatusList.get().then((res) => {
				if (res.code == 0) {
					this.orderStatusList = res.data['order_status'];
					this.payStatusList = res.data['pay_status'];
				}
			}).catch((msg) => {
				this.$alert(msg, "提示", {type: 'error'})
			})
		},
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		//编辑
		handleData(mode, row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open(mode).setData(row)
			})
		},
		//支付
		payOrder(mode, row) {
			this.paydialog.save = true
			this.$nextTick(() => {
				this.$refs.paysaveDialog.open(mode).setData(row)
			})
		},
		//查看
		showData(mode, row) {
			this.$nextTick(() => {
				this.$router.push({
					path: '/company/order/detail',
					query: {
						company_order_sn: row.company_order_sn,
						action: 'view'
					}
				})
			})
		},
		//取消
		cancelOrder(mode, row) {
			console.log(mode, row);
			this.$confirm(`注：确认取消订单吗？`, '提示', {
				type: 'warning',
				confirmButtonText: '确定',
				confirmButtonClass: 'el-button--danger'
			}).then(() => {
				this.$API['company.company_order'].cancelOrder.post({company_order_sn: row.company_order_sn})
					.then((res) => {
						if (res.code == 0) {
							this.$refs.table.refresh()
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					})
					.catch((msg) => {
						this.$alert(msg, "提示", {type: 'error'})
					})
					.finally(() => {

					});
			}).catch(() => {
			})
		},
		closeOrder(mode, row) {
			console.log(mode, row);
			this.$confirm(`订单关闭之后，订单中未使用的服务将不能使用，请谨慎操作`, '提示', {
				type: 'warning',
				confirmButtonText: '确定',
				confirmButtonClass: 'el-button--danger'
			}).then(() => {
				this.$API['company.company_order'].closeOrder.post({company_order_sn: row.company_order_sn})
					.then((res) => {
						if (res.code == 0) {
							this.$refs.table.refresh()
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					})
					.catch((msg) => {
						this.$alert(msg, "提示", {type: 'error'})
					})
					.finally(() => {

					});
			}).catch(() => {
			})
		},
		//下载激活码
		downLoadCode(mode, row) {
			console.log(mode, row);
			this.$API['company.company_order'].downLoadCode.get({company_order_sn: row.company_order_sn}).then((res) => {
				window.open(res.data.download_url);
			}).catch((msg) => {
				this.$alert(msg, "提示", {type: 'error'})
			})
		},
		importUser(mode, row) {
			this.$router.push({
				path: '/company/order/import_user',
				query: {
					company_order_sn: row.company_order_sn,
				}
			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//重置
		resetsearch() {
			this.dateTo = [];
			this.search = {
				company_id: null,
				product_name: null,
				card_name: null,
				order_status: null,
				pay_status: null,
				created_at_begin: null,
				created_at_end: null,
			}
		},
		//本地更新数据
		handleSuccess() {
			this.dialog.save = false
			this.paydialog.save = false
			this.$refs.table.reload()
		},
	}
}
</script>

<style>
</style>
